﻿Imports System.Drawing.Printing

Public Class Clientes
    Dim nombre, dni, direccion, observaciones As String
    Dim telefono As Integer

    'NO DEJAR QUE LA PANTALLA SE MUEVA
    Private Sub Clientes_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        Me.CenterToScreen()
    End Sub

    'LOAD DE CLIENTES
    Private Sub Clientes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.CitasTableAdapter.Fill(Me.NuevoZooDBDataSet.Citas)

        Me.AnimalesTableAdapter.Fill(Me.NuevoZooDBDataSet.Animales)

        Me.AnimalesTableAdapter.Fill(Me.NuevoZooDBDataSet.Animales)

        Me.ClientesTableAdapter.Fill(Me.NuevoZooDBDataSet.Clientes)
    End Sub



    'BÚSQUEDAS DE LOS DATAGRID
    Private Sub TxtNombre_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtNombre.TextChanged
        Me.ClientesTableAdapter.FillByNombre(NuevoZooDBDataSet.Clientes, TxtNombre.Text)
    End Sub

    Private Sub TxtDNI_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtDNI.TextChanged
        Me.ClientesTableAdapter.FillByDNI(NuevoZooDBDataSet.Clientes, TxtDNI.Text)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.AnimalesTableAdapter.Fill(Me.NuevoZooDBDataSet.Animales)
    End Sub
    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        Me.ClientesTableAdapter.FillByNombre(Me.NuevoZooDBDataSet.Clientes, TextBox2.Text)
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Me.ClientesTableAdapter.FillByDNI(Me.NuevoZooDBDataSet.Clientes, TextBox1.Text)
    End Sub

    Private Sub TextBox16_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox16.TextChanged
        Me.ClientesTableAdapter.FillByNombre(Me.NuevoZooDBDataSet.Clientes, TextBox16.Text)
    End Sub

    Private Sub TextBox15_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox15.TextChanged
        Me.ClientesTableAdapter.FillByDNI(Me.NuevoZooDBDataSet.Clientes, TextBox15.Text)
    End Sub



    'BOTON DE AÑADIR UN CLIENTE NUEVO
    Private Sub BAddCli_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BAddCli.Click
        nombre = TxtAddNom.Text
        dni = TxtAddDNI.Text
        Integer.TryParse(TxtAddTlf.Text, telefono)
        direccion = TxtAddDir.Text
        observaciones = TxtAddObs.Text

        Dim query As IEnumerable(Of Decimal) = _
             From item As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
             Where item.Cells(0).Value IsNot DBNull.Value _
             Select CDec(item.Cells(0).Value)

        ' Obtenemos el valor máximo
        Dim id As Integer = query.ToList.Max() + 1

        If Login.validar.ValidarNombre(nombre) <> 3 Or nombre = "" Then
            MsgBox("El nombre introducido no es correcto.", , "Error")
        ElseIf dni = "" Or Login.validar.ValidarDNI(dni) <> 3 Then
            MsgBox("El DNI introducido no es correcto.", , "Error")
        ElseIf Login.validar.ValidarTlf(telefono) <> 3 Then
            MsgBox("Teléfono incorrecto", , "Error")
        ElseIf id = 0 Then
            MsgBox("Debe seleccionar un registro de la tabla", , "Error")
        Else
            Try
                Me.ClientesTableAdapter.Insert(id, dni, nombre, telefono, direccion, observaciones)
                Me.ClientesTableAdapter.Fill(Me.NuevoZooDBDataSet.Clientes)
            Catch ex As Exception
                MsgBox("No se pudo guardar los datos")
            End Try
        End If
    End Sub

    'BOTON DE EDITAR LOS DATOS
    Private Sub BEdCli_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BEdCli.Click

        Dim id As Integer

        Dim dr As New DataGridViewRow
        dr = DataGridView3.SelectedRows(0)
        id = dr.Cells(0).Value.ToString
        nombre = TxtEdNom.Text

        dni = TxtEdDNI.Text
        telefono = CInt(TxtEdTlf.Text)
        direccion = TxtEdDir.Text
        observaciones = TxtEdObs.Text

        If Login.validar.ValidarNombre(nombre) <> 3 Or nombre = "" Then
            MsgBox("Nombre incorrecto", , "Error")
        ElseIf Login.validar.ValidarDNI(dni) <> 3 Or dni = "" Then
            MsgBox("DNI incorrecto", , "Error")
        ElseIf Login.validar.ValidarTlf(telefono) <> 3 Or dni = "" Then
            MsgBox("Teléfono incorrecto", , "Error")
        Else

            Try
                Me.ClientesTableAdapter.UpdateQuery(dni, nombre, telefono, direccion, observaciones, id)
                Me.ClientesTableAdapter.Fill(Me.NuevoZooDBDataSet.Clientes)
                MsgBox("Registro actualizado correctamente", , "Información")
            Catch ex As Exception
                MsgBox("No se pudo guardar los datos")
            End Try
        End If
    End Sub

    

    'BOTÓN DE ELIMINAR CLIENTE
    Private Sub BElCli_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BElCli.Click
        Dim id As Integer
        Dim dr As New DataGridViewRow
        dr = DataGridView3.SelectedRows(0)
        id = dr.Cells(0).Value.ToString

        Dim resultado As MsgBoxResult = MsgBox("¿Desea eliminar el registro seleccionado?", MsgBoxStyle.YesNo, "Confirmacion de borrado")
        If resultado = MsgBoxResult.Yes Then
            Try
                Me.ClientesTableAdapter.Delete(id)
                Me.ClientesTableAdapter.Fill(Me.NuevoZooDBDataSet.Clientes)
            Catch ex As Exception
                MsgBox("No se pudo eliminar los datos")
            End Try
        End If
    End Sub


    'CAMBIAR LOS TEXTBOX SEGUN HACES CLIC ENCIMA, EN EL DATAGRID DE MODIFICAR
    Private Sub DataGridView3_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView3.CellClick
        Dim dr As New DataGridViewRow
        dr = DataGridView3.SelectedRows(0)
        TxtEdNom.Text = dr.Cells(2).Value.ToString
        TxtEdDNI.Text = dr.Cells(1).Value.ToString
        TxtEdTlf.Text = dr.Cells(3).Value.ToString
        TxtEdDir.Text = dr.Cells(4).Value.ToString
        TxtEdObs.Text = dr.Cells(5).Value.ToString
    End Sub

    'CAMBIAR LOS TEXTBOX SEGUN HACES CLIC ENCIMA, EN EL DATAGRID DE CONSULTAR
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim dr As New DataGridViewRow
        dr = DataGridView1.SelectedRows(0)
        TextBox8.Text = dr.Cells(0).Value.ToString
        TextBox4.Text = dr.Cells(1).Value.ToString
        TextBox3.Text = dr.Cells(2).Value.ToString
        TextBox5.Text = dr.Cells(3).Value.ToString
        TextBox6.Text = dr.Cells(4).Value.ToString
        TextBox7.Text = dr.Cells(5).Value.ToString
    End Sub

    'CAMBIAR LOS TEXTBOX SEGUN HACES CLIC ENCIMA, EN EL DATAGRID DE ELIMINAR
    Private Sub DataGridView4_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView4.CellClick

        Dim dr As New DataGridViewRow
        dr = DataGridView3.SelectedRows(0)
        TextBox9.Text = dr.Cells(0).Value.ToString
        TextBox13.Text = dr.Cells(1).Value.ToString
        TextBox14.Text = dr.Cells(2).Value.ToString
        TextBox12.Text = dr.Cells(3).Value.ToString
        TextBox11.Text = dr.Cells(4).Value.ToString
        TextBox10.Text = dr.Cells(5).Value.ToString

    End Sub



    'BOTÓN PARA IMPRIMIR EL CARNET
    Private Sub BCarnet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BCarnet.Click
        AddHandler PrintDocument1.PrintPage, AddressOf Me.Imprimir
        PrintDocument1.Print()

    End Sub
    'METODO PARA IMPRIMIR EL CARNET
    Private Sub Imprimir(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim nombrecarnet As String
        If TextBox3.Text.Length > 22 Then
            nombrecarnet = TextBox3.Text.Substring(0, 21)
        Else
            nombrecarnet = TextBox3.Text
        End If
        Dim ruta As String = "../../Resources/Carnet.png"
        ev.Graphics.DrawImage(Image.FromFile(ruta), 0, 0, 300, 190)
        ev.Graphics.DrawString("ID Cliente", New Font("Calibri", 11, FontStyle.Bold), Brushes.DarkSlateGray, 10, 94)
        ev.Graphics.DrawString(TextBox8.Text, New Font("Calibri", 11, FontStyle.Bold), Brushes.DarkSlateGray, 105, 94)
        ev.Graphics.DrawString("Nombre", New Font("Calibri", 11, FontStyle.Bold), Brushes.DarkSlateGray, 10, 125)
        ev.Graphics.DrawString(nombrecarnet, New Font("Calibri", 11, FontStyle.Bold), Brushes.DarkSlateGray, 105, 125)
        ev.Graphics.DrawString("DNI", New Font("Calibri", 11, FontStyle.Bold), Brushes.DarkSlateGray, 10, 154)
        ev.Graphics.DrawString(TextBox4.Text, New Font("Calibri", 11, FontStyle.Bold), Brushes.DarkSlateGray, 105, 154)
        ev.HasMorePages = False   'Si hay más de un folio a imprimir hay que ponerlo a true
    End Sub

    
    Private Sub NuevoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NuevoToolStripMenuItem.Click
        Consultar.SelectedIndex = 1
    End Sub
    Private Sub SalirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalirToolStripMenuItem.Click
        Me.Close()
        Gestion.Show()


    End Sub
    Private Sub WebsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WebsToolStripMenuItem.Click
        Webs.Show()
    End Sub
    Private Sub EliminarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        Consultar.SelectedIndex = 3
    End Sub
    Private Sub EditarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditarToolStripMenuItem.Click
        Consultar.SelectedIndex = 2
    End Sub
   
    Private Sub AyudaToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AyudaToolStripMenuItem1.Click
        Dim rutaEjecutable As String = System.IO.Directory.GetCurrentDirectory().ToString
        System.Diagnostics.Process.Start(rutaEjecutable & "\Ayuda.chm")
    End Sub
End Class
